#ifndef __TM1638_H__
#define __TM1638_H__

#include "stm32f10x.h"
#include "gpio.h"
#include "sys.h"
#include "currency.h"

typedef struct tm1638_struct{
  GPIO_TypeDef*    CLK_GPIOx;      	//SCL的端口
  u16   CLK_PIN_x;       			//SCL的引脚
  GPIO_TypeDef*    DIO_GPIOx;      	//SDA的端口
  u16   DIO_PIN_x;       			//SDA的引脚
  GPIO_TypeDef*    STB_GPIOx;      	//SDA的端口
  u16   STB_PIN_x;       			//SDA的引脚
}TM1638_TypeDef;

extern TM1638_TypeDef tm1638;;
extern u8 DispTab[4];
extern u8 KeyValue[4];  			//保存按键值
extern u8 Update_disp_flg;

//#define TM1638_STB(t,x)              		gpio_set(t->STB_GPIOx,t->STB_PIN_x,x);
//#define TM1638_CLK(t,x)              		gpio_set(t->CLK_GPIOx,t->CLK_PIN_x,x);
//#define TM1638_DIO_IN(t)              	gpio_init(t->DIO_GPIOx,t->DIO_PIN_x,GPIO_Mode_Out_PP); 
//#define TM1638_DIO_OUT(t)                 gpio_init(t->DIO_GPIOx,t->DIO_PIN_x,GPIO_Mode_IPU);             
//#define TM1638_DIO_DataOUT(t,x)        	gpio_set(t->DIO_GPIOx,t->DIO_PIN_x,x);
//#define TM1638_DIO_DataIN(t)          	gpio_read_in(t->DIO_GPIOx,t->DIO_PIN_x)

#define TM1638_STB(t,x)              gpio_set(t->STB_GPIOx,t->STB_PIN_x,x);
#define TM1638_CLK(t,x)              gpio_set(t->CLK_GPIOx,t->CLK_PIN_x,x);
#define TM1638_DIO_IN(t)              	 gpio_init(t->DIO_GPIOx,t->DIO_PIN_x,GPIO_Mode_IPU);
#define TM1638_DIO_OUT(t)               gpio_init(t->DIO_GPIOx,t->DIO_PIN_x,GPIO_Mode_Out_PP);             
#define TM1638_DIO_DataOUT(t,x)        gpio_set(t->DIO_GPIOx,t->DIO_PIN_x,x);
#define TM1638_DIO_DataIN(t)          	 gpio_read_in(t->DIO_GPIOx,t->DIO_PIN_x)


//地址命令设置
#define TM1638_GRID1  0xc0
#define TM1638_GRID2  0xc1
#define TM1638_GRID3  0xc2
#define TM1638_GRID4  0xc3
#define TM1638_GRID5  0xc4
#define TM1638_GRID6  0xc5
#define TM1638_GRID7  0xc6
#define TM1638_GRID8  0xc7

#define TM1638_GRID9  0xc8
#define TM1638_GRID10 0xc9
#define TM1638_GRID11 0xca
#define TM1638_GRID12 0xcb
#define TM1638_GRID13 0xcc
#define TM1638_GRID14 0xcd
#define TM1638_GRID15 0xce
#define TM1638_GRID16 0xcf

#define		GRID(x)	(x-1)*2
#define 	SEG(x)	1<<(x-1)

/****************************************************************************/
//显示面板右边显示LED灯状态控制
#define		LED_OverVol_R(t)				Digital_led(&tm1638,GRID(7),SEG(1),t)
#define		LED_OverVol_G(t)				Digital_led(&tm1638,GRID(7),SEG(2),t)

#define		LED_LowVol_R(t)					Digital_led(&tm1638,GRID(7),SEG(3),t)
#define		LED_LowVol_G(t)					Digital_led(&tm1638,GRID(7),SEG(4),t)

#define		LED_LeakCurr_R(t)				Digital_led(&tm1638,GRID(7),SEG(5),t)
#define		LED_LeakCurr_G(t)				Digital_led(&tm1638,GRID(7),SEG(6),t)

#define		LED_OverCurr_R(t)				Digital_led(&tm1638,GRID(7),SEG(7),t)
#define		LED_OverCurr_G(t)				Digital_led(&tm1638,GRID(7),SEG(8),t)

#define		LED_CAUTION_R(t)				Digital_led(&tm1638,GRID(7),SEG(9),t)
#define		LED_CAUTION_G(t)				Digital_led(&tm1638,GRID(7),SEG(10),t)


//显示面板左边显示LED灯状态控制
#define		LED_Pow_R(t)				Digital_led(&tm1638,GRID(8),SEG(1),t)
#define		LED_Pow_G(t)				Digital_led(&tm1638,GRID(8),SEG(2),t)

#define		LED_Run_R(t)				Digital_led(&tm1638,GRID(8),SEG(3),t)
#define		LED_Run_G(t)				Digital_led(&tm1638,GRID(8),SEG(4),t)

#define		LED_Net_R(t)				Digital_led(&tm1638,GRID(8),SEG(5),t)
#define		LED_Net_G(t)				Digital_led(&tm1638,GRID(8),SEG(6),t)

#define		LED_WIFI_R(t)				Digital_led(&tm1638,GRID(8),SEG(7),t)
#define		LED_WIFI_G(t)				Digital_led(&tm1638,GRID(8),SEG(8),t)

#define		LED_GPS_R(t)				Digital_led(&tm1638,GRID(8),SEG(9),t)
#define		LED_GPS_G(t)				Digital_led(&tm1638,GRID(8),SEG(10),t)

// 输出控制  显示LED灯状态控制  8
#define		LED_ONU_R(t)				Digital_led(&tm1638,GRID(5),SEG(1),t)
#define		LED_ONU_G(t)				Digital_led(&tm1638,GRID(5),SEG(2),t)
                                        
#define		LED_PBX_R(t)			    Digital_led(&tm1638,GRID(5),SEG(3),t)
#define		LED_PBX_G(t)			    Digital_led(&tm1638,GRID(5),SEG(4),t)
                                        
#define		LED_FAN_R(t)			    Digital_led(&tm1638,GRID(5),SEG(5),t)
#define		LED_FAN_G(t)			    Digital_led(&tm1638,GRID(5),SEG(6),t)
                                        
#define		LED_HEAT_R(t)			    Digital_led(&tm1638,GRID(5),SEG(7),t)
#define		LED_HEAT_G(t)			    Digital_led(&tm1638,GRID(5),SEG(8),t)

#define		LED_AC1_R(t)				Digital_led(&tm1638,GRID(6),SEG(1),t)
#define		LED_AC1_G(t)				Digital_led(&tm1638,GRID(6),SEG(2),t)
                                        
#define		LED_AC2_R(t)				Digital_led(&tm1638,GRID(6),SEG(3),t)
#define		LED_AC2_G(t)				Digital_led(&tm1638,GRID(6),SEG(4),t)
                                        
#define		LED_AC3_R(t)				Digital_led(&tm1638,GRID(6),SEG(5),t)
#define		LED_AC3_G(t)				Digital_led(&tm1638,GRID(6),SEG(6),t)
                                        
#define		LED_AC4_R(t)				Digital_led(&tm1638,GRID(6),SEG(7),t)
#define		LED_AC4_G(t)				Digital_led(&tm1638,GRID(6),SEG(8),t)			

/*******************************end*********************************************/



/*************************新版动环控制面板led灯**********************************************/

#define		LED_RUN_R(t)					Digital_led(&tm1638,GRID(5),SEG(1),t)   //运行灯
#define		LED_NET_R(t)					Digital_led(&tm1638,GRID(5),SEG(2),t)   //网络灯
#define		LED_IO1_R(t)					Digital_led(&tm1638,GRID(5),SEG(3),t)   //IO-1灯
#define		LED_IO2_R(t)					Digital_led(&tm1638,GRID(5),SEG(6),t)   //IO-2灯

#define		LED_OverVol(t)					Digital_led(&tm1638,GRID(5),SEG(5),t)   //重合闸指示灯
#define		LED_LowVol(t)					Digital_led(&tm1638,GRID(5),SEG(4),t)
#define		LED_LeakCurr(t)					Digital_led(&tm1638,GRID(5),SEG(7),t)
#define		LED_OverCurr(t)					Digital_led(&tm1638,GRID(5),SEG(10),t) 

//#define		LED_AC1_R(t)				Digital_led(&tm1638,GRID(5),SEG(5),t)   //继电器1
//#define		LED_AC2_R(t)				Digital_led(&tm1638,GRID(5),SEG(4),t)   //继电器2
//#define		LED_AC3_R(t)				Digital_led(&tm1638,GRID(5),SEG(10),t)  //继电器3
//#define		LED_AC4_R(t)				Digital_led(&tm1638,GRID(5),SEG(7),t)   //继电器4

/*******************************end*********************************************/



void Read_key(TM1638_TypeDef *TM1638,u8 *KeyValue);
void Digital_led_reg(TM1638_TypeDef *TM1638,u8 dig,u16 seg,u8 onoff);
void TM1638_Init(TM1638_TypeDef *TM1638);
void Digital_led(TM1638_TypeDef *TM1638,u8 dig,u16 seg,u8 onoff);


#endif

